package zen2d.example.sanguo;

import zen2d.*;
import zen2d.SwingWindow;
import zen2d.util.AssertUtil;

import java.awt.*;
import java.util.Random;

public class XuChu extends Sprite {

    Animation animation;

    Animation beAttack;

    Animation current;
    Animation walk;

    int step = 0;
    Random r = new Random();

    private Direction direction;
    int speed = 5;

    public XuChu(int x, int y, int width, int height) {
        super(x, y, width, height);

         animation = new Animation();
         animation.setKeyFrames(AssertUtil.gifToImages("image/sanguo/xuchu.gif"));
         animation.setDuration(100);

         beAttack = new Animation();
         beAttack.setKeyFrames(AssertUtil.gifToImages("image/sanguo/xuchubd.gif"));
         beAttack.setDuration(100);
         beAttack.setPlayMode(Animation.PlayMode.NORMAL);
         current = animation;

         walk = new Animation();
         walk.setKeyFrames(AssertUtil.gifToImages("image/sanguo/xuchu-walk.gif"));
         walk.setDuration(100);

         direction = Direction.S;


    }

    @Override
    protected void afterEndLive() {

    }

    @Override
    protected void update() {

        Attack attack = Zen2d.getCollisionWithClass(this,Attack.class);
        if (null != attack) {

            if (attack.isSameHorizontal(this)){

                beAttack.reset();
                current = beAttack;
//            Window.addTask(2000,()->{
//                current = animation;
//            });
                return;
            }
        }
        if (beAttack.isFinished()){
            current = animation;
        }


//        switch (direction){
//            case L:{
//                x -= speed;
//            }break;
//            case R:{
//                x+=speed;
//            }break;
//            case U:{
//                y -= speed;
//            }break;
//            case D:{
//                y+= speed;
//            }break;
//            case S:{
//
//            }break;
//        }
//        if (direction != Direction.S){
//            current = walk;
//        }
//
//        borderDetection();
//
//        if (step == 0){
//            step = r.nextInt(22)+3;
//            Direction [] dirs = Direction.values();
//            int rn = r.nextInt(dirs.length);
//            direction = dirs[rn];
//        }
//        step --;



    }

    @Override
    protected void draw(GraphicsContext graphicsContext) {
        graphicsContext.drawImage(current.getKeyFrame(),x,y,width,height);
        graphicsContext.setColor(Color.PINK);
        graphicsContext.drawRect(0,y+height-HORIZONTAL_SIZE, Zen2d.WIDTH,HORIZONTAL_SIZE,false);
    }

    @Override
    protected void up() {

    }

    @Override
    protected void down() {

    }

    @Override
    protected void left() {

    }

    @Override
    protected void right() {

    }

    @Override
    protected void space() {

    }
}
